package com.kcnet.qk.controller.dynamic;

import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.kcnet.qk.constant.Const.LIST_VIEW;

/**
 * Created by Administrator on 2018/5/4 0004.
 */

public class TemeLineSendAllFriendMsgCTR extends BasePeopleController {
    private static String TAG = "TemeLineSendAllFriendMsgCTR";
    private String content;
    private static TemeLineSendAllFriendMsgCTR releaseimgctr;
    protected Map<String, Boolean> mRecordMap = Collections.synchronizedMap(new HashMap());

    private boolean isClickUser;
    public static TemeLineSendAllFriendMsgCTR getInstance() {
        if (releaseimgctr == null) {
            synchronized (TemeLineSendAllFriendMsgCTR.class) {
                if (releaseimgctr == null) {
                    releaseimgctr = new TemeLineSendAllFriendMsgCTR();
                }
            }
        }
        return releaseimgctr;
    }


    @Override
    public void doTask(AccessibilityEvent event) {

        mClassName = (String) event.getClassName();
        mType = event.getEventType();
        if (mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.MSGLauncherUI)) {

            tagTextClick("第4/4个选项卡");
            sleepTimedDefault();
            initFbMain =true;
        }
        else if(initFbMain && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))){
            clickSelectGroupFist();
        }else if(isClickUser){
            isClickUser = false;
            RootShell.click(0 ,900);
            sleepTime(1500);
            inputSayHiSentence(content);
            tagTextClick("发送");
            RootShell.click(20 ,40);
            initFbMain = true;
        }
    }

    public void clickSelectGroupFist() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    // ToastUtil.showToast(AppContext.getInstance().getContext(), "scrllorNodeInfo listView null");
                    return;
                }
                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {

                    if (currentAddCount == maxAddCount) {
                        currentAddCount = 0;
                        stopTask();
                        break;
                    }

                    AccessibilityNodeInfo nodeInfo = scrllorNodeInfo.getChild(q);
                    Log.d("HTML","nodeInfo.getChildCount():"+nodeInfo.getChildCount());
                    if(nodeInfo.getChildCount() > 1){
                        String keyName ="";
                        try {
                            List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId("@id/contact_name");
                            Log.d("HTML","list:"+list.size());
                            keyName = list.get(0).getContentDescription().toString();
                        }
                        catch (Exception e){
                            e.printStackTrace();
                        }
                        if(mRecordMap.containsKey(keyName)){
                            continue;
                        }
                        isClickUser = NodeUtil.performClick(nodeInfo);
                        if(isClickUser){
                            mRecordMap.put(keyName,true);
                            currentAddCount++;
                            initFbMain = false;
                            Log.d("ND", "点击了" +isClickUser+" keyName："+keyName);
                            break;
                        }
                    }
                    if (q == scrllorNodeInfo.getChildCount() - 1) {
                        if (scrllorNodeInfo != null) {
                            NodeUtil.performScroller(scrllorNodeInfo);
                            break;
                        }
                    }

                }
            }

        }
    }

    public void setParms(int maxAddCount,int minAddTime,int maxAddTime,String content){
        //  stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.maxAddCount = maxAddCount;
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.content = content;
    }
}
